﻿@model IEnumerable<Commission.Models.Customer>
@using MyResources = Commission.Resources.AppResource

@{
    ViewBag.Title = @MyResources.LabelCustomer;
}

@section MyJavascript {
    <script type="text/javascript">
    $(document).ready(function () {
        $('#UserName').autocomplete(
        {
            source: '@Url.Action("CustomerSearch", "Customer")'
        }
        );
    })
    </script>
}

@section Sortable {
    @Scripts.Render("~/bundles/sortable")
}

<h3>@MyResources.LabelCustomerList</h3>

@using (Html.BeginForm("Index", "Customer", FormMethod.Get))
{
<p>
    @MyResources.LabelCustomer : @Html.TextBox("UserName", ViewBag.CurrentFilter as string,
          new { @class = "form-control searchtext" })

    @MyResources.LabelCustomerType :
        @Html.DropDownList(
            "CustomerTypeID",
            (IEnumerable<SelectListItem>)ViewBag.CustomerTypeIDs, string.Empty,
              new { @class = "form-control searchtext", @style = "width:216px" }
            )

    <input type="submit" value="@MyResources.LabelSearch" style="height:28px" />

    @if (ViewBag.CanCreateDirect != null && ViewBag.CanCreateDirect)
    {
        @Html.Raw("　")
        @Html.ActionLink(@MyResources.LinkCreateDirectCustomer, "Create", new { isDirectCustomer = true })
    }
    else
    {
        if (User.IsInRole("Admin"))
        {
            @Html.Raw("　")
            @Html.ActionLink(@MyResources.LinkCreateNew, "Create")
            @Html.Raw(" | ")
            @Html.ActionLink(@MyResources.LinkGetPoint, "GetPoint", new { id = 0 })
            @Html.Raw(" | ")
            @Html.ActionLink(@MyResources.LinkExportToExcel, "ExportToExcel", new { id = 0 })
        }
    }
</p>
}

<table class="table hoverTable sortable">
    <tr>
        <th>
            @MyResources.LabelUserName
        </th>
        <th>
            @MyResources.LabelCustomerName
        </th>
        <th>
            @MyResources.LabelCustomerType
        </th>
        <th>
            @MyResources.LabelSponsorCustomer
        </th>
        <th>
            @MyResources.LabelParentCustomer
        </th>
        <th class="text-center" style="width:116px;">
            @MyResources.LabelAttendantDate
        </th>
        <th class="text-center" style="width:110px;">
            @MyResources.LabelValidDate
        </th>

        @if (User.IsInRole("Admin"))
        {
            <th></th>
        }
        else if (ViewBag.CanCreateDirect != null && ViewBag.CanCreateDirect)
        {
            <th></th>
        }
    </tr>

    @foreach (var item in Model)
    {
        <tr style="padding:0px; margin:0px;">
            <td >
                @Html.ActionLink(item.UserName, "Details", new { id = item.CustomerID })
            </td>

            <td >
                @Html.DisplayFor(modelItem => item.Name)
            </td>

            <td >
                @Html.DisplayFor(modelItem => item.CustomerType.TypeName)
            </td>

            <td >
                @if (item.SponsorCustomer != null)
                {
                    @Html.ActionLink(item.SponsorCustomer.DisplayName, "Details",
                        new { id = item.SponsorCustomer.CustomerID })
                }
            </td>

            <td >
                @if (item.ParentCustomer != null)
                {
                    @Html.ActionLink(item.ParentCustomer.DisplayName, "Details",
                        new { id = item.ParentCustomer.CustomerID })
                }
            </td>

            <td class="text-center" >
                @Html.DisplayFor(modelItem => item.AttendantDate)
            </td>

            <td class="text-center" >
                @Html.DisplayFor(modelItem => item.ValidDate)
            </td>

            @if (User.IsInRole("Admin"))
            {
                <td>
                    @Html.ActionLink(@MyResources.LabelResetPassword, "ResetPassword", new { userName = item.UserName }) |
                    @Html.ActionLink(@MyResources.LinkEdit, "Edit", new { id = item.CustomerID }) |
                    @Html.ActionLink(@MyResources.LinkDelete, "Delete", new { id = item.CustomerID }) |
                    @Html.ActionLink(@MyResources.LinkGetPoint, "GetPoint", new { id = item.CustomerID })

                    @if (item.IsCanUpgrade)
                    {
                        @Html.Raw(" | ")
                        @Html.ActionLink(@MyResources.LabelUpgrade, "Upgrade",
                            new { customerId = item.CustomerID, isDirectCustomer = false })
                    }
                    else
                    {
                    }
                </td>
            }
            else if (ViewBag.CanCreateDirect != null && ViewBag.CanCreateDirect)
            {
                <td>
                    @if (item.IsCanUpgrade) { 
                        @Html.ActionLink(@MyResources.LabelUpgrade, "Upgrade",
                            new { customerId = item.CustomerID, isDirectCustomer = true })
                    }
                    else
                    {
                        
                    }
                </td>
            }

        </tr>
    }

</table>
